package com.sx.core;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sx.core.utils.HttpClientInstance;
import com.sx.core.utils.Md5;
import com.sx.core.utils.PropertyUtils;
import com.sx.core.utils.Sha;
import org.apache.commons.collections.map.CaseInsensitiveMap;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO: DOCUMENT ME!
 *
 * @author   <a href="mailto:lihao@ozstrategy.com">Hao Li</a>
 * @version  02/14/2017 19:52
 */
public interface CardConstants {
  /**
   * 用户名密码加密因子
   */
  public static final String password_salt="Passw0rd!";
  public static final String password_algorithmName="md5";


  int SERVER_ERROR_CODE=10000;
  String ONE_CARD_RESULT_SUCCESS_CODE="000000";
  String ONE_CARD_RESULT_SUCCESS_CODE_7="000007";
  String OperCodeSno="01";
  String OperCodeIdcard="04";
  /**
   * 登录验证
   */
  String ONE_CARD_REQUEST_100001="100001";

  /**
   * 钱包充值
   */
  String ONE_CARD_REQUEST_100003="100003";
  /**
   * 充值校验
   */
  String ONE_CARD_REQUEST_200003="200003";


  /**
   * 电控交费
   */
  String ONE_CARD_REQUEST_100005="100005";

  /**
   * 卡片挂失
   */
  String ONE_CARD_REQUEST_100007="100007";

  /**
   * 卡片解挂
   */
  String ONE_CARD_REQUEST_100009="100009";

  /**
   * 个人信息查询
   */
  String ONE_CARD_REQUEST_1000011="100011";
  /**
   * 消费流水查询
   */
  String ONE_CARD_REQUEST_1000013="100013";
  /**
   * 校区查询
   */
  String ONE_CARD_REQUEST_1000019="100019";
  /**
   * 楼栋查询
   */
  String ONE_CARD_REQUEST_1000021="100021";

  /**
   * 楼栋查询
   */
  String ONE_CARD_REQUEST_1000051="100051";



  /**
   * 宿舍查询
   */
  String ONE_CARD_REQUEST_1000023="100023";

  /**
   * 应收交费
   */
  String ONE_CARD_REQUEST_1000029="100029";
  /**
   * 应收欠费查询
   */
  String ONE_CARD_REQUEST_1000027="100027";

  /**
   * 学杂费查询
   */
  String ONE_CARD_REQUEST_1000035="100035";

  /**
   * 学杂费交费
   */
  String ONE_CARD_REQUEST_1000037="100037";

  /**
   * 水费交费
   */
  String ONE_CARD_REQUEST_1000039="100039";
  /**
   * 1.1	购电(水)记录查询(根据人员)
   */
  String ONE_CARD_REQUEST_1000017="100017";
  /**
   * 1.1	购电(水)记录查询（根据房间号）
   */
  String ONE_CARD_REQUEST_1000053="100053";



  /**
   * 1.1	校内代收已交费查询
   */
  String ONE_CARD_REQUEST_1000045="100045";

  /**
   * 1.1	学费已交费查询（天才）
   */
  String ONE_CARD_REQUEST_1000047="100047";
  /**
   * 1.1	获取照片（天才）
   */
  String ONE_CARD_REQUEST_1000049="100049";
  /**
   * 获取流水类型
   */
  String ONE_CARD_REQUEST_1000055="100055";

  /**
   * 选收缴费查询
   */
  String ONE_CARD_REQUEST_100031="100031";
  /**
   * 选收缴费
   */
  String ONE_CARD_REQUEST_100033="100033";









  public enum Plats{
    S001("001","手机APP"),//手机APP
    S002("002","PC网页"),//PC网页
    S003("003","微信公众号"),//微信公众号
    S004("004","POS终端"),//POS终端
    S005("005","综合收费平台")//综合收费平台
    ;
    private String code;
    private String description;

    Plats(String code,String description) {
      this.code = code;
      this.description=description;
    }

    public String getCode() {
      return code;
    }

    public String getDescription() {
      return description;
    }
    public static boolean contains(String name){
      if(name==null){
        return false;
      }
      Plats[] season = values();
      for(Plats s : season){
        if(s.getCode().equals(name)){
          return true;
        }
      }
      return false;
    }
  }
  public enum PayWays {
    S001("001","现金","0"),//天财收费系统
    S002("002","银行卡","0"),//校园一卡通系统
    S003("003","支付宝","0"),//综合收费平台
    S004("004","微信","1"),//网费系统
    S005("005","翼支付","0"),//水控系统
    S006("006","移动和包","0"),//电控系统
    S007("007","银联支付","0"),//扫码支付系统
    S008("008","签约银行卡","0"),//校园虚拟卡
    S009("009","建行聚合支付","0"),//校园虚拟卡
    S010("010","银行批扣","0"),//校园虚拟卡
    S011("011","工行E缴费","0"),//校园虚拟卡
    S012("012","工行线上POS","0"),//校园虚拟卡
    S013("013","中行E校园","0"),//校园虚拟卡
    S014("014","虚拟卡账户余额","0")//校园虚拟卡
    ;
    private String code;
    private String description;
    private String reconc;

    PayWays(String code,String description,String reconc) {
      this.code = code;
      this.description=description;
      this.reconc=reconc;
    }

    public String getCode() {
      return code;
    }
    public String getDescription() {
      return description;
    }

    public String getReconc() {
      return reconc;
    }
    public static boolean contains(String name){
      if(name==null){
        return false;
      }
      PayWays[] season = values();
      for(PayWays s : season){
        if(s.getCode().equals(name)){
          return true;
        }
      }
      return false;
    }
    public static List<Map<String,Object>> asList(){
      List<Map<String,Object>> mapList=new ArrayList<>();
      PayWays[] season = values();
      for(PayWays s : season){
        Map<String,Object> map=new HashedMap();
        map.put("code",s.getCode());
        map.put("name",s.getDescription());
        mapList.add(map);
      }
      return mapList;
    }
  }
  public enum Contexts {
    S001("001","天财收费系统","0"),//天财收费系统
    S002("002","校园一卡通系统","1"),//校园一卡通系统
    S003("003","综合收费平台","0"),//综合收费平台
    S004("004","网费系统","0"),//网费系统
    S005("005","水控系统","0"),//水控系统
    S006("006","电控系统","0"),//电控系统
    S007("007","扫码支付系统","0"),//扫码支付系统
    S008("008","校园虚拟卡","0"),//校园虚拟卡
    S009("009","建行消费机","0")//校园虚拟卡
    ;
    private String code;
    private String description;
    private String syncFlag;//是否同步人员

    Contexts(String code,String description,String syncFlag) {
      this.code = code;
      this.description=description;
      this.syncFlag=syncFlag;
    }

    public String getCode() {
      return code;
    }
    public String getDescription() {
      return description;
    }

    public String getSyncFlag() {
      return syncFlag;
    }
    public static boolean contains(String name){
      if(name==null){
        return false;
      }
      Contexts[] season = values();
      for(Contexts s : season){
        if(s.getCode().equals(name)){
          return true;
        }
      }
      return false;
    }
  }








  class Result implements Serializable {
    private String Code;
    private String SchoolCode;
    private String EncodeModel;
    private String Token;
    private String Nonce;
    private String Timestamp;
    private String SHA1;
    private String MsgCode;
    private String Message;
    private Map Body=new HashMap();

    public Result() {
    }
    public String getCode() {
      return Code;
    }

    public void setCode(String code) {
      Code = code;
    }

    public String getSchoolCode() {
      return SchoolCode;
    }

    public void setSchoolCode(String schoolCode) {
      SchoolCode = schoolCode;
    }

    public String getEncodeModel() {
      return EncodeModel;
    }

    public void setEncodeModel(String encodeModel) {
      EncodeModel = encodeModel;
    }

    public String getToken() {
      return Token;
    }

    public void setToken(String token) {
      Token = token;
    }

    public String getNonce() {
      return Nonce;
    }

    public void setNonce(String nonce) {
      Nonce = nonce;
    }

    public String getTimestamp() {
      return Timestamp;
    }

    public void setTimestamp(String timestamp) {
      Timestamp = timestamp;
    }

    public String getSHA1() {
      return SHA1;
    }

    public void setSHA1(String SHA1) {
      this.SHA1 = SHA1;
    }

    public String getMsgCode() {
      return MsgCode;
    }

    public void setMsgCode(String msgCode) {
      MsgCode = msgCode;
    }

    public String getMessage() {
      return Message;
    }

    public void setMessage(String message) {
      Message = message;
    }

    public Map getBody() {
      return Body;
    }

    public void setBody(Map body) {
      Body = body;
    }

    public Result body(Map object){
      this.Body=object;
      return this;
    }
    public Result header(JSONObject jsonObject){
      this.Code=jsonObject.getString("Code");
      this.SchoolCode=jsonObject.getString("SchoolCode");
      this.EncodeModel=jsonObject.getString("EncodeModel");
      this.Token=jsonObject.getString("Token");
      this.Nonce=jsonObject.getString("Nonce");
      this.Timestamp=jsonObject.getString("Timestamp");
      this.SHA1=jsonObject.getString("SHA1");
      this.MsgCode=jsonObject.getString("MsgCode");
      this.Message=jsonObject.getString("Message");
      return this;
    }

  }

  String Token= Md5.md5("token");
  String Nonce= Token;
  default Result post(Map<String, Object> header, Map<String, Object> body){
    header.put("SchoolCode", PropertyUtils.getProperty("card.school.code"));
    header.put("Token",Token);
    header.put("Nonce",Nonce);
    String Timestamp=System.currentTimeMillis()+"";
    header.put("Timestamp",Timestamp);
    header.put("SHA1", Sha.sha(Token+Nonce+Timestamp));
    Map<String,Object> params=new HashMap<>();
    params.put("Head",header);
    params.put("Body",body);
    String str = HttpClientInstance.getInstance().postJson(PropertyUtils.getProperty("card.url"),params);
    Result result=null;
    if(StringUtils.isNotBlank(str)){
      JSONObject jsonObject= JSON.parseObject(str);
      JSONObject Body=jsonObject.getJSONObject("Body");
      CaseInsensitiveMap map=null;
      if(Body!=null){
        map=new CaseInsensitiveMap(jsonObject.getJSONObject("Body"));
      }else{
        map=new CaseInsensitiveMap();
      }
      result=new Result().header(jsonObject.getJSONObject("Head")).body(map);
    }else{
      result=new Result();
      result.setMessage("接口异常");
      result.setMsgCode("111111");
    }
    return result;
  }
  default String postResultString(Map<String, Object> header, Map<String, Object> body){
    header.put("SchoolCode",PropertyUtils.getProperty("card.school.code"));
    Map<String,Object> params=new HashMap<>();
    params.put("Head",header);
    params.put("Body",body);
    String str = HttpClientInstance.getInstance().postJson(PropertyUtils.getProperty("card.url"),params);
    return str;
  }


} // end class Constants
